package com.huixh.train.business.service.impl;

import com.huixh.train.business.domain.ConfirmOrder;
import com.huixh.train.business.domain.DailyTrainSeat;
import com.huixh.train.business.domain.DailyTrainTicket;
import com.huixh.train.business.enums.ConfirmOrderStatusEnum;
import com.huixh.train.business.fegin.MemberFegin;
import com.huixh.train.business.mapper.ConfirmOrderMapper;
import com.huixh.train.business.mapper.DailyTrainSeatMapper;
import com.huixh.train.business.mapper.cust.DailyTrainTicketMapperCust;
import com.huixh.train.business.req.ConfirmOrderTicketReq;
import com.huixh.train.common.req.MemberTicketReq;
import com.huixh.train.common.resp.Result;
import jakarta.annotation.Resource;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

/**
* @author huixh
*/
@Service
public class AfterConfirmOrderServiceImpl{
    private static final Logger logger = LoggerFactory.getLogger(AfterConfirmOrderServiceImpl.class);
    @Resource
    private DailyTrainSeatMapper dailyTrainSeatMapper;
    @Resource
    private MemberFegin memberFegin;
    @Resource
    private ConfirmOrderMapper confirmOrderMapper;
    @Resource
    private DailyTrainTicketMapperCust dailyTrainTicketMapperCust;
    //@GlobalTransactional
    public void afterDoConfirmOrder(DailyTrainTicket dailyTrainTicket, List<DailyTrainSeat> finalSeatList, List<ConfirmOrderTicketReq> tickets, ConfirmOrder confirmOrder){
        // logger.info("开启全局事务{}", RootContext.getXID());

        for (int j = 0; j < finalSeatList.size(); j++) {
            DailyTrainSeat dailyTrainSeat = finalSeatList.get(j);
            DailyTrainSeat seatForUpdate = new DailyTrainSeat();
            seatForUpdate.setId(dailyTrainSeat.getId());
            seatForUpdate.setSell(dailyTrainSeat.getSell());
            seatForUpdate.setUpdateTime(new Date());
            dailyTrainSeatMapper.updateByPrimaryKeySelective(seatForUpdate);

            Integer startIndex = dailyTrainTicket.getStartIndex();

            Integer endIndex = dailyTrainTicket.getEndIndex();
            char[] charArray = seatForUpdate.getSell().toCharArray();
            Integer maxStartIndex = endIndex-1;
            Integer minEndIndex = startIndex+1;
            Integer minStartIndex = 0;

            for(int i = startIndex; i>= 0; i--){
                char c = charArray[i];
                if(c == '1'){
                    maxStartIndex = i;
                    break;
                }
            }
            Integer maxEndIndex = seatForUpdate.getSell().length();
            for(int i = endIndex; i< seatForUpdate.getSell().length(); i++){
                char c = charArray[i];
                if(c == '1'){
                    maxEndIndex = i;
                    break;
                }
            }

            dailyTrainTicketMapperCust.updateCountBySell(
                    dailyTrainSeat.getDate(),
                    dailyTrainSeat.getTrainCode(),
                    dailyTrainSeat.getSeatType(),
                    minStartIndex,
                    maxStartIndex,
                    minEndIndex,
                    maxEndIndex
            );

            MemberTicketReq memberTicketReq = new MemberTicketReq();
            memberTicketReq.setMemberId(confirmOrder.getMemberId());

            memberTicketReq.setPassengerId(tickets.get(j).getPassengerId());
            memberTicketReq.setPassengerName(tickets.get(j).getPassengerName());
            memberTicketReq.setTrainDate(dailyTrainTicket.getDate());
            memberTicketReq.setTrainCode(dailyTrainTicket.getTrainCode());
            memberTicketReq.setCarriageIndex(dailyTrainSeat.getCarriageIndex());
            memberTicketReq.setSeatRow(dailyTrainSeat.getRow());
            memberTicketReq.setSeatCol(dailyTrainSeat.getCol());
            memberTicketReq.setStartStation(dailyTrainTicket.getStart());
            memberTicketReq.setStartTime(dailyTrainTicket.getStartTime());
            memberTicketReq.setEndStation(dailyTrainTicket.getEnd());
            memberTicketReq.setEndTime(dailyTrainTicket.getEndTime());
            memberTicketReq.setSeatType(dailyTrainSeat.getSeatType());
            Result<String> result = memberFegin.save(memberTicketReq);
            logger.info("调用member接口，返回：{}", result);

            // 更新订单状态为成功
            ConfirmOrder confirmOrderForUpdate = new ConfirmOrder();
            confirmOrderForUpdate.setId(confirmOrder.getId());
            confirmOrderForUpdate.setUpdateTime(new Date());
            confirmOrderForUpdate.setStatus(ConfirmOrderStatusEnum.SUCCESS.getCode());
            confirmOrderMapper.updateByPrimaryKeySelective(confirmOrderForUpdate);
        }
    }
}